package com.ssm.controller;

import com.ssm.pojo.Demand;
import com.ssm.pojo.UserInformation;
import com.ssm.service.imp.DemandOperationImp;
import com.ssm.utils.UserContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class DemandOperation {

    @Resource(name = "DemandOperationImp")
    private DemandOperationImp demandOperationImp;

    /**
    * 发布需求
    * */
    @RequestMapping(value="/addDemand")
    @ResponseBody
    public Map addDemand(String sCode,String tCode,String eCode,String title,String content,Float price,String time){
        //  state 0,成功，1、失败，2、传入参数为空
        HashMap<Object, Object> data = new HashMap<>();
        if(sCode == null || tCode == null || eCode == null || title == null || price == null || time == null){
            data.put("state",2);
        }else{
            Demand demand = new Demand();
            demand.setsCode(sCode);
            demand.settCode(tCode);
            demand.seteCode(eCode);
            demand.setTitle(title);
            demand.setContent(content);
            demand.setPrice(price);
            demand.setTime(time);
            try {
                Boolean aBoolean = demandOperationImp.addDemand(demand);
                data.put("state",0);
            }catch (Exception e){
                data.put("state",1);
            }
        }
        return data;
    }

    /**
     * 获取需求列表，初始化数据
     *  @param pageSize {int} 第几页
     *  @param num {int} 每页的数量
     *  @return {Map} state 0,成功，1、失败，2、传入参数异常，3、账号或者密码错误,4、查询异常
     * */
    @RequestMapping(value = "/initDemandData")
    @ResponseBody
    public Map initDemandData(Integer pageSize,Integer num){
        HashMap<Object, Object> demandData = new HashMap<>();
        int size = demandOperationImp.demandListSize();
        if(pageSize != null && num != null && pageSize > 0 && num > 0){
            pageSize = pageSize - 1;
            if(pageSize*num < size){
                try {
                    List<Demand> demands = demandOperationImp.demandList(pageSize*num, num);
                    demandData.put("state",0);
                    demandData.put("data",demands);
                }catch (Exception e){
                    demandData.put("state","4");
                }
            }else{
                demandData.put("state","2");
            }
        }else{
            demandData.put("state","2");
        }
        return demandData;
    }
    /**
     * 选中需求
     * @param setId {}
     * @return {Map} state 0,成功，1、失败，2、传入参数为空,3、已经有人选定,4、选中id等于了自己的id
     * */
    @RequestMapping(value = "/responseDemand")
    @ResponseBody
    public Map responseDemand(Long setId) {
        HashMap<Object, Object> data = new HashMap<>();
        Long id = ((UserInformation) UserContext.getUser()).getId();
        if(id != null && setId != null){
            if(!id.equals(setId)){
                try{
                    Integer aBoolean = demandOperationImp.responseDemand(setId,id);
                    if(aBoolean > 0){
                        data.put("state",0);
                    }else{
                        data.put("state",3);
                    }
                }catch (Exception e){
                    data.put("state",1);
                }
            }else{

            }
        }else{
            data.put("state",2);
        }
        return data;
    }
}
